import json
import os
import time

from Base.BaseSetting import Template, Report_dir


def output_report(resultData):
    """生成测试报告到指定路径下"""
    template_path = Template

    with open(template_path, 'rb') as file:
        body = file.readlines()

    filename = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))+'report'+'.html'
    with open(Report_dir + filename, 'wb') as write_file:
        for item in body:
            if item.strip().startswith(b'var resultData'):
                head = '    var resultData = '
                item = item.decode().split(head)
                item[1] = head + json.dumps(resultData, ensure_ascii=False, indent=4)
                item = ''.join(item).encode()
                item = bytes(item) + b';\n'
            write_file.write(item)